package com.ruoyi.flowable.listener.repair;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.equ.domain.EquRepair;
import com.ruoyi.equ.service.IEquRepairService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruoyi.flowable.listener.asset.AssetListener_2.getFileList;

/**
 * 维修1-申请监听
 */
@Slf4j
@Component("repairStartListener")
public class RepairStartListener implements ExecutionListener {
    private static final ObjectMapper objectMapper = new ObjectMapper();
    private static final IEquRepairService repairService = SpringUtils.getBean(IEquRepairService.class);

    @Override
    public void notify(DelegateExecution execution) {
        try {
            //关联资产id
            Long assetId = Long.valueOf(execution.getVariable("assetId", String.class));

            //故障信息：故障时间+现象+错误代码<如有>+影响范围+紧急程度，如“2025-01-15 10:00+无法开机+无+影响单个诊疗+紧急”）
            //faultTime+faultType+(errorCode)+faultImpact+urgency
            String faultTime = execution.getVariable("faultTime", String.class);
            String faultType = execution.getVariable("faultType", String.class);
            String errorCode = execution.getVariable("errorCode", String.class);
            String faultImpact = execution.getVariable("faultImpact", String.class);
            String urgency = execution.getVariable("urgency", String.class);
            String remark = execution.getVariable("remark", String.class);

            HashMap<String, Object> faultInfoMap = new HashMap<>();
            faultInfoMap.put("故障时间", faultTime);
            faultInfoMap.put("故障现象", faultType);
            if (errorCode.equals("")) {
                faultInfoMap.put("错误代码", "无");
            }
            else {
                faultInfoMap.put("错误代码", errorCode);
            }
            faultInfoMap.put("影响范围", faultImpact);
            faultInfoMap.put("紧急程度", urgency);
            faultInfoMap.put("备注", remark);
            String faultInfo = objectMapper.writeValueAsString(faultInfoMap);

            //工单最终状态
            String orderStatus = "待派工";

            //附件链接
            List<String> faultFiles = getFileList(execution, "faultFiles");
            Map<String, Object> fileUrlsMap = new HashMap<>();
            fileUrlsMap.put("故障照片/视频", faultFiles);
            String fileUrls = objectMapper.writeValueAsString(fileUrlsMap);

            //创建者
            String createBy = SecurityUtils.getLoginUser().getUser().getNickName();

            //赋值
            EquRepair equRepair = new EquRepair();
            equRepair.setAssetId(assetId);
            equRepair.setFaultInfo(faultInfo);
            equRepair.setOrderStatus(orderStatus);
            equRepair.setFileUrls(fileUrls);
            equRepair.setCreateBy(createBy);

            //新增维修
            repairService.insertEquRepair(equRepair);
            //存json
            execution.setVariable("equRepair", equRepair);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
